var COS = require('../../lib/cos-wx-sdk-v5');
const app = getApp()

Page({
  data: {
    TabCur: 0,
    PreCur: 0,
    cardCur: 0,
    scrollLeft: 0,
    takeSession: false,
    tabs:[],
    start: 0,
    curSortId: "",
    list: [],
  },

  onLoad: function() {
    this.getSorts()
    
  },
  getSorts:function(){
    var that = this
    app.request.reqCloudLoading("getSorts", {
    }, function (res) {
      if (res.code == 0 && res.data.length != 0) {
        for (var i = 0; i < res.data.length; i++) {
          that.data.tabs.push(res.data[i])
        }
        that.setData({
          tabs: that.data.tabs,
        })
        console.log("that.data.tabs====" + JSON.stringify(that.data.tabs))
        that.getList(0, that.data.tabs[0]._id)
        that.data.curSortId = that.data.tabs[0]._id
      } else {

      }
      console.log("====" + JSON.stringify(res))
    })
  },
  getList(start, sortId) {
    var that = this
    app.request.reqCloudLoading("getPics", {
      start: start,
      sortId: sortId
    }, function(res) {
      if (res.code == 0 && res.data.length != 0) {
        for (var i = 0; i < res.data.length; i++) {
          that.data.list.push(res.data[i])
        }
        that.setData({
          list: that.data.list,
          start: (start + 1)
        })
      } else {

      }
      console.log("====" + JSON.stringify(res))
    })
  },
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  bindClickItem: function(e) {
    console.log("======" + JSON.stringify(e))
    var index = e.currentTarget.dataset.index
    console.log("===" + encodeURIComponent(JSON.stringify(this.data.list[index])))
    wx.navigateTo({
      url: '/pages/pic/detail?object=' + encodeURIComponent(JSON.stringify(this.data.list[index])),
    })
  },
  onPullDownRefresh: function() {
    this.data.start = 0
    this.getList(0, this.data.curSortId)
  },
  onReachBottom: function() {
    this.getList(this.data.start, this.data.curSortId)
  },
  tabSelect(e) {
    var left = (e.currentTarget.dataset.id - 1) * 60
    var curTabIndex = e.currentTarget.dataset.id
    this.setData({
      list: [],
      curSortId: this.data.tabs[curTabIndex]._id,
      start: 0,
      TabCur: curTabIndex,
      scrollLeft: left
    })
    this.data.list = []
    this.data.curSortId = this.data.tabs[curTabIndex]._id
    this.getList(0, this.data.tabs[curTabIndex]._id)
  },
  onShareAppMessage: function () {
    var length = this.data.list.length
    var random = Math.floor(Math.random() * length)
    var imageUrl = this.data.list[random].imgs[0]
    return {
      title: '美图欣赏，欢迎加入',
      imageUrl: imageUrl,
      path: '/pages/index/index'
    }
  }
})